//
//  luoerStatusOriginalView.swift
//  WeiBoStudy
//
//  Created by luoerliu on 16/5/20.
//  Copyright © 2016年 刘文平. All rights reserved.
//

import UIKit
import SDWebImage
class luoerStatusOriginalView: UIView {
    var status: luoerStatus?{
        didSet
        {
            // 3.cell给原创view设置了模型,有模型后可以给原创view的子控件设置内容
            
            // 头像
            if let urlString = status?.user?.profile_image_url {
                let url = NSURL(string: urlString)!
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            // 名称
            let name = status?.user?.screen_name
            nameLabel.text = name
//            print("\(name)")
            // 时间
            timeLabel.text = status?.created_at
            
            // 来源
            sourceLabel.text = status?.source
//            print("\(status?.source)")
            // 会员等级
            if let mbrank = status?.user?.mbrank {
                if mbrank > 0 && mbrank <= 6 {
                    // 有会员等级.设置相应的图片
                    // 拼接图片common_icon_membership_level会员等级
                    let imageName = "common_icon_membership_level\(mbrank)"
                    memberView.image = UIImage(named: imageName)
                } else {
                    // mbrank = 0,将图片设置为nil
                    memberView.image = nil
                }
            } else {
                // 没有, 将图片设置为nil
                memberView.image = nil
            }
            
            // 认证类型
            if let verifiedType = status?.user?.verified_type {
                // 没有认证:-1   认证用户:0  企业认证:2,3,5  达人:220
                switch verifiedType {
                case 0:
                    verifiedView.image = UIImage(named: "avatar_vip")
                case 2, 3, 5:
                    verifiedView.image = UIImage(named: "avatar_enterprise_vip")
                case 220:
                    verifiedView.image = UIImage(named: "avatar_grassroot")
                default:
                    // 没有认证类型
                    verifiedView.image = nil
                }
            } else {
                // 没有认证类型
                verifiedView.image = nil
            }
//            微博内容
            contentLabel.text = status?.text
//            print("\(status?.text)")
         }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame:CGRect)
    {
        super.init(frame:frame)
        self.prepareUI()
    }
    private func prepareUI()
    {
          let CellMargin: CGFloat = 8
        backgroundColor = UIColor.whiteColor()
//        添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        //    添加约束
        /// 用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(self).offset(CellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        /// 用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CellMargin)
            make.top.equalTo(iconView)
        }
        
        /// 会员等级图标
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        /// 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        /// 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        /// 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 14, height: 14))
            
        }
        /// 微博文本内容label
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(12)
        }
        
        /// 设置原创view的底部和contentLabel的底部重合偏移8
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentLabel).offset(8)
        }

    }
    
//    MARK: ----懒加载控件
//    用户头像
    private lazy var iconView: UIImageView = UIImageView(image:UIImage(named: "avatar"))
//    用户名称
    private lazy var nameLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
//    时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.orangeColor()
        return label
    }()
    /// 来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        
        // 设置label大小
        label.font = UIFont.systemFontOfSize(15)
        
        // 设置颜色
        label.textColor = UIColor.lightGrayColor()
        
        return label
    }()
//    会员等级
    private lazy var memberView: UIImageView = UIImageView(image: nil)
    private lazy var verifiedView: UIImageView = UIImageView(image: nil)
//    微博文本内容

    private lazy var contentLabel: UILabel =
    {
        let label = UILabel()
        let CellStatusFontSize: CGFloat = 14
        // label大小
        label.font = UIFont.systemFontOfSize(CellStatusFontSize)
        // 颜色
        label.textColor = UIColor.darkGrayColor()
        
        // 多行
        label.numberOfLines = 0
        
        // 最大换行宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * 8
        
        return label
    }()
}
